package com.ruoyi.quartz.task;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.plasma.domain.PlasmaUncollectionStatistics;
import com.ruoyi.plasma.service.IPlasmaUncollectionStatisticsService;
import com.ruoyi.tongji.run.manager.ManagerFactory;
import com.ruoyi.tongji.utils.DateUtil;
import com.spire.ms.System.Collections.ArrayList;
import org.apache.commons.lang3.math.NumberUtils;
import org.beetl.core.misc.NumberUtil;
import org.beetl.sql.core.SQLManager;
import org.beetl.sql.core.SQLReady;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.ruoyi.common.utils.StringUtils;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * 定时任务调度测试
 * 
 * @author ruoyi
 */
@Component("ryTask")
public class RyTask
{
    @Autowired
    private IPlasmaUncollectionStatisticsService plasmaUncollectionStatisticsService;

    public void ryMultipleParams(String s, Boolean b, Long l, Double d, Integer i)
    {
        System.out.println(StringUtils.format("执行多参方法： 字符串类型{}，布尔类型{}，长整型{}，浮点型{}，整形{}", s, b, l, d, i));
    }

    public void ryParams(String params)
    {
        System.out.println("执行有参方法：" + params);
    }

    public void ryNoParams()
    {
        System.out.println("执行无参方法：" + DateUtil.format(new Date(), "HH:mm:ss"));
    }

    public void overNumberHandleTask()
    {
        System.out.println("过号浆员处理：" + DateUtil.format(new Date(), "HH:mm:ss"));
        String dateStart = DateUtil.format(new Date(), "yyyyMMdd");
        String dateEnd = DateUtil.format(new Date(), "yyyyMMdd");
        //
        PlasmaUncollectionStatistics plasmaUncollectionStatistics = new PlasmaUncollectionStatistics();
        String riqi = DateUtil.format(new Date(),"yyyy-MM-dd");
        Date 日期 = DateUtil.getDate(riqi);
        plasmaUncollectionStatistics.set日期(日期);
        //plasmaUncollectionStatistics.setType("0");//过号未采的字典值是0
        List<PlasmaUncollectionStatistics> list = plasmaUncollectionStatisticsService.selectPlasmaUncollectionStatisticsList(plasmaUncollectionStatistics);
        System.out.println("已记录当日过号浆员：" + list.size() + "名");
        //搜索过号超过2个小时的浆员列表
        SQLManager taibangSqlManager = ManagerFactory.taibangSqlManager();
        String unCollectionSql = "select ROUND(TIMESTAMPDIFF(MINUTE, 登记时间, 当前时间)/60,1) AS interval_time,a.* from (" +
                "select f.processNo, DATE_FORMAT(r.registerDate,'%Y%m%d%H%i%s') as 登记时间, NOW() as 当前时间,f.plasmaCollection," +
                "d.* " +
                "from t_bims_donor_collect_flow f " +
                "left join t_bims_donor_register r on f.donateNo = r.donateNo  " +
                "left join t_bims_donor d on f.donorId = d.donorId " +
                "where " +
                "f.recordDate between ? and ? and f.plasmaCollection != 'Y' order by r.registerDate asc) as a";
        List<HashMap> unCollectList = taibangSqlManager.execute(new SQLReady(unCollectionSql, new Object[] {dateStart, dateEnd}), HashMap.class);
        List<String> unCollectDonorIdList = new ArrayList();
        for (HashMap hashMap : unCollectList) {
            PlasmaUncollectionStatistics pu = new PlasmaUncollectionStatistics();

            //判断这个过号未采的是否已记录
            pu.set日期(日期);
            pu.setProcessNo((Integer)hashMap.get("processno"));
            List<PlasmaUncollectionStatistics> l = plasmaUncollectionStatisticsService.selectPlasmaUncollectionStatisticsList(pu);
            if (l.size() != 0) {
                pu = l.get(0);
            }
            pu.setDonorNo((String)hashMap.get("donorno"));
            pu.setDonorId((String)hashMap.get("donorid"));
            pu.setDonorName((String)hashMap.get("donorname"));
            pu.setReason((String)hashMap.get("cancelreason"));
            pu.setIntervalTime(((BigDecimal)hashMap.get("intervalTime")).toString());
            pu.setDonorState((String)hashMap.get("donorstate"));
            pu.setProcessNo((Integer)hashMap.get("processno"));
            pu.setPhone((String)hashMap.get("phone"));
            pu.setPlasmaCollection((String)hashMap.get("plasmacollection"));
            pu.setType("0");//过号未采，默认都是
            if(StringUtils.equals(pu.getPlasmaCollection(),"I")){
                pu.setReason("脂浆");
                pu.setType("3");
            }
            if(StringUtils.contains(pu.getReason(),"ALT")){
                pu.setType("8");
            }
            //如果已记录过，更新一下记录信息。如果没有记录过，插入一条记录
            if (pu.getId() != null) {
                this.plasmaUncollectionStatisticsService.updatePlasmaUncollectionStatistics(pu);
            } else {
                pu.setInsertTime(DateUtil.format(new Date(),"yyyyMMddHHmmss"));
                this.plasmaUncollectionStatisticsService.insertPlasmaUncollectionStatistics(pu);
            }

            unCollectDonorIdList.add(pu.getDonorId());
        }
        //获取今天已记录过的列表
        PlasmaUncollectionStatistics pu = new PlasmaUncollectionStatistics();
        pu.set日期(日期);//查询条件是当天记录的所有记录
        List<PlasmaUncollectionStatistics> resultList = plasmaUncollectionStatisticsService.selectPlasmaUncollectionStatisticsList(pu);
        //如果查询到的未采浆员记录不在朗程查询到的列表里，说明该浆员已经采浆，不用重复记录。

        for (PlasmaUncollectionStatistics uncollectionStatistics : resultList) {
            if (unCollectDonorIdList.contains(uncollectionStatistics.getDonorId())) {
                //如果有记录，则不用处理
            } else {
                //如果没有记录，则把献浆状态改为Y
                uncollectionStatistics.setPlasmaCollection("Y");
                this.plasmaUncollectionStatisticsService.updatePlasmaUncollectionStatistics(uncollectionStatistics);
            }
        }
    }
}
